package com.isunland.managesystem.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.ui.AppointerListActivity;
import com.isunland.managesystem.ui.CustomerListChooseActivity;
import com.isunland.managesystem.ui.CustomerNameDialogFragment;
import com.isunland.managesystem.ui.MaterielKindDialogFragment;
import com.isunland.managesystem.ui.WareHouseDialogFragment;
import com.isunland.managesystem.utils.LogUtil;

public abstract class BaseOrderSaleDetailFragment extends BaseFragment implements OnClickListener {

  protected static final int CHOOSE_COMPANY = 0;
  protected static final int CHOOSE_WAREHOUSE = 1;
  protected static final int CHOOSE_PERSON = 2;

  protected static final int EDITE_ORDER = 3;
  protected static final int DELETE_ORDER = 4;
  protected static final int SUBMIT_ORDER = 5;
  protected static final int NO_SUBMITE_ORDER = 6;
  protected static final int SAVE_ORDER = 7;
  protected static final int SAVE = 8;//添加保存
  protected static final int SAVE_START = 9;//添加 保存并启动流程
  protected static final int MATERIAL_KIND = 10;//选择 出库物料类别

  protected TextView tvCustomerCompany;
  protected ImageView ibCustomerCompany;
  protected EditText etCustomerApartment;
  protected EditText etMainProduct;
  protected TextView tvMaterialKind;//出库物料类别
  protected ImageView ibMaterialKind;
  protected TextView tvWarehouse;//出货仓库
  protected ImageView ibWarehouse;
  protected TextView tvSalePerson;//销售业务员
  protected ImageView ibSalePerson;
  protected TextView tvOutNotes;//出库物品说明
  protected TextView tvRegisteredErson;//登记人
  protected TextView tvRegisteredTime;//登记时间
  protected TextView tvDataStatus;//数据状态

  protected TextView tvHandlePrson;//办理人
  protected TextView tvHandleTime;//办理时间
  protected EditText etNotes;//备注
  protected LinearLayout llDataStatus;
  protected LinearLayout llHandlePrson;
  protected LinearLayout llHandleTime;
  protected LinearLayout llGone;

  protected String salePersonName = "";
  protected String salePersonId = "";
  protected String customerName = "";
  protected String customerId = "";
  protected String warehouseName = "";
  protected String warehouseId = "";
  protected String materialKindCode = "";
  protected String materialKindName = "";

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle();
  }

  /**
   * 设置标题
   */
  protected abstract void setTitle();

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_order_detail, container, false);

    tvCustomerCompany = (TextView) v.findViewById(R.id.tv_customer_company);
    ibCustomerCompany = (ImageView) v.findViewById(R.id.ib_customer_company);
    etCustomerApartment = (EditText) v.findViewById(R.id.et_customer_apartment);
    etMainProduct = (EditText) v.findViewById(R.id.et_main_product);
    tvMaterialKind = (TextView) v.findViewById(R.id.tv_material_kind);//出库物料类别
    ibMaterialKind = (ImageView) v.findViewById(R.id.ib_material_kind);
    tvWarehouse = (TextView) v.findViewById(R.id.tv_warehouse);
    ibWarehouse = (ImageView) v.findViewById(R.id.ib_warehouse);
    tvSalePerson = (TextView) v.findViewById(R.id.tv_sale_person);
    ibSalePerson = (ImageView) v.findViewById(R.id.ib_sale_person);
    tvOutNotes = (TextView) v.findViewById(R.id.tv_out_notes);
    tvRegisteredErson = (TextView) v.findViewById(R.id.tv_registered_erson);
    tvRegisteredTime = (TextView) v.findViewById(R.id.tv_registered_time);
    tvDataStatus = (TextView) v.findViewById(R.id.tv_data_status);

    tvHandlePrson = (TextView) v.findViewById(R.id.tv_handle_person);
    tvHandleTime = (TextView) v.findViewById(R.id.tv_handle_time);
    etNotes = (EditText) v.findViewById(R.id.et_notes);
    llDataStatus = (LinearLayout) v.findViewById(R.id.ll_data_status);
    llHandlePrson = (LinearLayout) v.findViewById(R.id.ll_handle_person);
    llHandleTime = (LinearLayout) v.findViewById(R.id.ll_handle_time);

    llGone = (LinearLayout) v.findViewById(R.id.ll_gone);
    ibCustomerCompany.setOnClickListener(this);
    ibMaterialKind.setOnClickListener(this);
    ibWarehouse.setOnClickListener(this);
    ibSalePerson.setOnClickListener(this);

    initOrderData();
    return v;
  }

  /**
   * 初始化控件
   */
  protected abstract void initOrderData();

  @Override public void onClick(View v) {
    switch (v.getId()) {
      //选择客户单位的dialog
      case R.id.ib_customer_company:
        //			showDialog(CHOOSE_COMPANY);
        //跳转到客户名录列表界面
        Intent i = new Intent(getActivity(), CustomerListChooseActivity.class);
        startActivityForResult(i, CHOOSE_COMPANY);
        break;
      //选择出库物料类别
      case R.id.ib_material_kind:
        showDialog(MATERIAL_KIND);
        break;
      //选择仓库的dialog
      case R.id.ib_warehouse:
        showDialog(CHOOSE_WAREHOUSE);
        break;
      //选择业务员的dialog
      case R.id.ib_sale_person:
        showDialog(CHOOSE_PERSON);
        break;

      default:
        break;
    }
  }

  @SuppressWarnings("static-access") protected void showDialog(int requestCode) {

    DialogFragment dialog = null;
    switch (requestCode) {
      //选择客户单位
      case CHOOSE_COMPANY:
        dialog = new CustomerNameDialogFragment();
        break;
      case CHOOSE_WAREHOUSE:
        dialog = new WareHouseDialogFragment();
        break;

      case CHOOSE_PERSON:
        Intent i = new Intent(getActivity(), AppointerListActivity.class);
        startActivityForResult(i, requestCode);
        return;

      case DELETE_ORDER:
        dialog = new BaseRestartDialogFragment().newInstance(R.string.deleteConfirm,
            R.string.restart_title);

        break;
      case SUBMIT_ORDER:
        dialog = new BaseRestartDialogFragment().newInstance(R.string.requisition_submit_hint,
            R.string.restart_title);
        break;
      case NO_SUBMITE_ORDER:
        dialog = new BaseRestartDialogFragment().newInstance(R.string.requisition_nosubmit_hint,
            R.string.restart_title);
        break;
      case SAVE_ORDER:
        dialog =
            new BaseRestartDialogFragment().newInstance(R.string.ifSave, R.string.restart_title);
        break;
      case SAVE_START:
        dialog = BaseConfirmDialogFragment.newInstance(R.string.cannotEdit, R.string.hintMessage);
        break;
      //出库物料类别
      case MATERIAL_KIND:
        dialog = new MaterielKindDialogFragment();
        break;

      default:
        break;
    }

    FragmentManager fm = getActivity().getSupportFragmentManager();
    if (dialog == null) return;
    dialog.setTargetFragment(BaseOrderSaleDetailFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    //客户单位
    if (requestCode == CHOOSE_COMPANY) {
      //			CustomerDialog customer = (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      //			customerName=customer.getName();
      //			customerId=customer.getId();
      //			tvCustomerCompany.setText(customerName);

      customerName = data.getStringExtra("customerName");
      customerId = data.getStringExtra("customerId");
      tvCustomerCompany.setText(customerName);
      return;
    }
    if (requestCode == MATERIAL_KIND) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      materialKindName = customer.getName();
      materialKindCode = customer.getId();
      tvMaterialKind.setText(materialKindName);
      return;
    }
    if (requestCode == CHOOSE_WAREHOUSE) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      warehouseName = customer.getName();
      warehouseId = customer.getId();
      tvWarehouse.setText(warehouseName);
      LogUtil.i("warehouseId==" + warehouseId);
      LogUtil.i("warehouseName==" + warehouseName);
      return;
    }
    if (requestCode == CHOOSE_PERSON) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      salePersonName = customer.getName();
      salePersonId = customer.getId();
      tvSalePerson.setText(salePersonName);
      return;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}
